//
//  StudyGroupsViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/11/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "StudyGroupsViewController.h"
#import "CoursesManager.h"
#import "WSError.h"
#import "Constants.h"


@interface StudyGroupsViewController ()

@end

@implementation StudyGroupsViewController

@synthesize  studyGroupsTable;

- (id)initWithNibName:(NSString *)nibNameOrNil booking:(Booking *)booking
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        // Custom initialization
        _booking = booking;
        
        if (_booking.course.allStudyGroups) {
            _studyGroups = _booking.course.studyGroups;
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (!_studyGroups || [_studyGroups count] == 1) {
        // Get Study Groups
        [Utils showLoadingOn:self];
        [[CoursesManager instance] getStudyGroupsForCourseCode:_booking.course.courseCode delegate:self];
        
    } else {
        [studyGroupsTable reloadData];
    }
}

#pragma mark - Table View Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_studyGroups count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    StudyGroup *studyGroup = [_studyGroups objectAtIndex:indexPath.row];
    if ([_booking.studyGroups containsObject:studyGroup]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = studyGroup.name;
    return cell;
}

#pragma mark - UITableView Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    StudyGroup *studyGroup = [_studyGroups objectAtIndex:indexPath.row];
    if ([_booking.studyGroups containsObject:studyGroup]) {
        [_booking.studyGroups removeObject:studyGroup];
    } else {
    [_booking.studyGroups addObject:[_studyGroups objectAtIndex:indexPath.row]];
    }
    [studyGroupsTable reloadData];
}

#pragma mark - CoursesProtocol Methods

- (void)didReceiveStudyGroupsWithSuccess:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen) {
        return;
    }
    _studyGroups = object;
    _booking.course.studyGroups = _studyGroups;
    _booking.course.allStudyGroups = YES;
    [studyGroupsTable reloadData];
}

- (void)didReceiveStudyGroupsWithFailure:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen) {
        return;
    }
    WSError *error = (WSError *)object;
    if (error.errorCode == StatusCodeConnectionError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:self cancelButtonTitle:NSLocalizedString(@"ALERT_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"ALERT_RETRY", nil), nil];
        [alertView show];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:nil cancelButtonTitle:NSLocalizedString(@"ALERT_OK", nil) otherButtonTitles: nil];
        [alertView show];
    }
}

#pragma mark - UIAlertViewDelegate Methods

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        // Get Study Groups
        [Utils showLoadingOn:self];
        [[CoursesManager instance] getStudyGroupsForCourseCode:_booking.course.courseCode delegate:self];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setStudyGroupsTable:nil];
    [super viewDidUnload];
}
@end
